package org.bluedream.core.module.sys.service.org;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.bluedream.comm.base.BaseServiceImpl;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.core.module.sys.dao.org.OrgRelationDao;
import org.bluedream.core.module.sys.entity.org.OrgRelation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * @ClassName OrgRelationService
 * @Description TODO
 * @Author foxsand
 * @Data 2021-07-06 14:12
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class OrgRelationService extends BaseServiceImpl<OrgRelationDao, OrgRelation> implements IService<OrgRelation> {
    private static final Logger logger = LoggerFactory.getLogger(OrgRelationService.class);

    @Override
    public OrgRelation getById(Serializable id) {
        return EmptyUtil.isEmpty(id)?new OrgRelation():super.getById(id);
    }

    public OrgRelation getOrgRelation(String orgId , String type , String entityId){
        QueryWrapper<OrgRelation> orgRelationQueryWrapper = new QueryWrapper<>();
        orgRelationQueryWrapper.eq("org_id" , orgId);
        orgRelationQueryWrapper.eq("type" , type);
        orgRelationQueryWrapper.eq("entity_Id" , entityId);
        OrgRelation orgRelation = getOne(orgRelationQueryWrapper);
        if (EmptyUtil.isEmpty(orgRelation)){
            orgRelation = new OrgRelation(orgId , type , entityId);
        }else {
            orgRelation.setIsNewRecord(false);
        }
        return orgRelation;
    }
}
